package com.hctforgreen.greenservice;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.hctforgreen.greenservice.model.MessageTopFirstEntity;
import com.hctforgreen.greenservice.model.MessageTopThirdEntity;
import com.hctforgreen.greenservice.model.SerieListEntity.SerieEntity;
import com.hctforgreen.greenservice.ui.adapter.RecentMessageListAdapter;
import com.hctforgreen.greenservice.ui.handler.RecentMessageListHandler;
import com.hctforgreen.greenservice.ui.widget.DefaultSeriesListPopupView;
import com.hctforgreen.greenservice.ui.widget.MessageTopFirstPopupView;
import com.hctforgreen.greenservice.ui.widget.MessageTopThirdPopupView;
import com.hctforgreen.greenservice.utils.ThemeStoreUtil;
import com.umeng.analytics.MobclickAgent;

/**
 * 通知一级页面 V1.5期
 * 
 * @author Tallen Zhai
 * 
 */
public class MessageFirstActivityV2 extends ParentActivity {
	/**
	 * 第一个下拉框
	 */
	private MessageTopFirstPopupView mFirstPopupView;
	/**
	 * 第二个下拉框
	 */
	private DefaultSeriesListPopupView mSecondPopupView;
	/**
	 * 第三个下拉框
	 */
	private MessageTopThirdPopupView mThirdPopupView;

	private MessageTopFirstEntity mCurrentFirstEntity;
	private SerieEntity mCurrentSecondEntity;
	private MessageTopThirdEntity mCurrentThirdEntity;
	private MessageFirstActivityV2 mSelf;
	private boolean bShowDataLst = false;

	private RecentMessageListAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_message_first_v2);
		mSelf = this;
		initSkinLayout();
		initTitleButtonBar();
		initWindow();
	}

	private void initTitleButtonBar() {
		final TextView titleTv = (TextView) findViewById(R.id.tv_title);
		titleTv.setText(getString(R.string.message_title_hint));

		final ImageButton backBtn = (ImageButton) findViewById(R.id.btn_back);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (getIntent().getBooleanExtra("fromMain", false)) {
					startActivity(new Intent(MessageFirstActivityV2.this,
							MainActivityV2.class));
					finish();
				} else {
					setResult(RESULT_OK);
					finish();
				}
			}
		});
	}

	private void initFirstBtn() {
		Button firstBtn = (Button) findViewById(R.id.btn_top_first);
		firstBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showListPopupView(v);
			}

			private void initListPopupView() {
				mFirstPopupView = new MessageTopFirstPopupView(
						MessageFirstActivityV2.this, mCurrentFirstEntity, mSelf);
			}

			private void showListPopupView(View v) {
				initListPopupView();
				mFirstPopupView.showAsDropDown(v);
			}
		});
	}

	private void initSecondBtn() {
		Button secondBtn = (Button) findViewById(R.id.btn_top_second);
		secondBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showListPopupView(v);
			}

			private void initListPopupView() {
				mSecondPopupView = new DefaultSeriesListPopupView(
						MessageFirstActivityV2.this, mCurrentSecondEntity,
						mSelf, bShowDataLst);
			}

			private void showListPopupView(View v) {
				initListPopupView();
				mSecondPopupView.showAsDropDown(v);
			}
		});
	}

	private void initThirdBtn() {
		Button thirdBtn = (Button) findViewById(R.id.btn_top_third);
		thirdBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showListPopupView(v);
			}

			private void initListPopupView() {
				mThirdPopupView = new MessageTopThirdPopupView(
						MessageFirstActivityV2.this, mCurrentThirdEntity, mSelf);
			}

			private void showListPopupView(View v) {
				initListPopupView();
				mThirdPopupView.showAsDropDown(v);
			}
		});
	}

	private void initWindow() {
		initTopBtn();
		initRecentMessageList();
	}

	private void initTopBtn() {
		initFirstBtn();
		initSecondBtn();
		initThirdBtn();
		initSearchBtn();
	}

	private void initSearchBtn() {
		final Button searchBtn = (Button) findViewById(R.id.btn_search);
		searchBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mCurrentFirstEntity == null) {
					Toast.makeText(MessageFirstActivityV2.this,
							R.string.message_top_first_hint, Toast.LENGTH_SHORT)
							.show();
					return;
				}

				String sDateFrom = (mCurrentThirdEntity == null) ? ""
						: mCurrentThirdEntity.dateFrom;
				String sType = mCurrentFirstEntity.type;
				String sSeriesId = (mCurrentSecondEntity == null) ? ""
						: mCurrentSecondEntity.id;
				EditText edtKey = (EditText) findViewById(R.id.et_input);

				String keywords = edtKey.getText().toString();

				doStartMessageListActivity(sDateFrom, sType, sSeriesId,
						keywords);
			}

			private void doStartMessageListActivity(final String dateFrom,
					final String type, final String seriesId,
					final String keywords) {
				MobclickAgent.onEvent(MessageFirstActivityV2.this,
						"Search_notification");
				Intent intent = new Intent(MessageFirstActivityV2.this,
						MessageListActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				intent.putExtra("dateFrom", dateFrom);
				intent.putExtra("type", type);
				intent.putExtra("seriesId", seriesId);
				intent.putExtra("keywords", keywords + "");
				startActivity(intent);
			}
		});
	}

	private void initRecentMessageList() {

		View convertView = findViewById(R.id.lyt_parent);
		ListView listView = (ListView) convertView
				.findViewById(R.id.lst_default_list);
		RecentMessageListHandler handler = new RecentMessageListHandler(this,
				convertView);
		mAdapter = new RecentMessageListAdapter(listView, this, convertView,
				R.layout.layout_loading, R.layout.layout_reloading, handler);
	}

	public void updateFirstEntity(MessageTopFirstEntity currentFirstEntity) {
		mCurrentFirstEntity = currentFirstEntity;

		if (currentFirstEntity.type.equals("2")) {
			bShowDataLst = true;

		} else {
			clearSecondEntity();
			clearSecondBtn();
			bShowDataLst = false;
		}
	}

	public void updateThirdEntity(MessageTopThirdEntity currentThirdEntity) {
		mCurrentThirdEntity = currentThirdEntity;

	}

	public void updateFirstBtn(MessageTopFirstEntity currentFirstEntity) {
		Button firstBtn = (Button) findViewById(R.id.btn_top_first);
		firstBtn.setText(currentFirstEntity.name);
	}

	public void updateThirdBtn(MessageTopThirdEntity currentThirdEntity) {
		Button thirdBtn = (Button) findViewById(R.id.btn_top_third);
		thirdBtn.setText(currentThirdEntity.name);
	}

	public void updateSecondBtn(SerieEntity currentEntity) {

		Button seriesBtn = (Button) findViewById(R.id.btn_top_second);
		seriesBtn.setText(currentEntity.name);
	}

	private void clearSecondBtn() {

		Button seriesBtn = (Button) findViewById(R.id.btn_top_second);
		seriesBtn.setText("");
	}

	private void clearSecondEntity() {

		mCurrentSecondEntity = null;
	}

	public void updateSecondEntity(SerieEntity currentEntity) {
		mCurrentSecondEntity = currentEntity;
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			setResult(RESULT_OK);

		}
		return super.onKeyDown(keyCode, event);

	}

	@Override
	protected void onDestroy() {
		if (mAdapter != null) {
			mAdapter.unRegisterBroadCast();
		}

		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		if (getIntent().getBooleanExtra("fromMain", false)) {
			startActivity(new Intent(MessageFirstActivityV2.this,
					MainActivityV2.class));
			finish();
		} else {
			setResult(RESULT_OK);
			finish();
		}
		super.onBackPressed();
	}
}
